package com.ecmcloud.utils;

import java.util.List;

import org.springframework.core.env.Environment;

import com.ecmcloud.common.dto.IndexPageStatistics;
import com.ecmcloud.common.dto.PageResultDto;
import com.ecmcloud.common.dto.ProcessInstanceParamDto;
import com.ecmcloud.common.dto.Result;
import com.ecmcloud.common.dto.TaskDto;
import com.ecmcloud.common.dto.TaskParamDto;
import com.ecmcloud.common.dto.TaskQueryParamDto;
import com.ecmcloud.common.dto.TaskResultDto;
import com.ecmcloud.common.dto.VariableQueryParamDto;
import com.ecmcloud.common.dto.flow.Node;
import com.ecmcloud.common.utils.CommonUtil;
import com.ecmcloud.common.utils.JsonMapperUtils;
import com.fasterxml.jackson.core.type.TypeReference;
import com.yomahub.tlog.hutoolhttp.TLogHutoolhttpInterceptor;

import cn.hutool.core.util.StrUtil;
import cn.hutool.extra.spring.SpringUtil;
import cn.hutool.http.HttpRequest;

public class CoreHttpUtil {
	
    private static TLogHutoolhttpInterceptor tLogHutoolhttpInterceptor = new TLogHutoolhttpInterceptor();

    public static String getBaseUrl() {
        Environment environment = SpringUtil.getBean(Environment.class);
        String bizUrl = environment.getProperty("core.url");
        return bizUrl;
    }

    @SuppressWarnings("unchecked")
    public static String post(Object object, String url) {
        String bizUrl =getBaseUrl();
		String post = HttpRequest.post(StrUtil.format("{}{}", bizUrl, url)).body(JsonMapperUtils.toJsonString(object)).addInterceptor(tLogHutoolhttpInterceptor).execute().body();
        return post;
    }

    @SuppressWarnings("unchecked")
    public static String get(String url) {
        String bizUrl = getBaseUrl();
        return HttpRequest.get(StrUtil.format("{}{}", bizUrl, url)).addInterceptor(tLogHutoolhttpInterceptor).execute().body();
    }
    
    /**
     * 查询任务变量
     * 全部都是
     *
     * @param taskId
     * @param keyList
     * @return
     */
    public static String queryTaskVariables(String taskId, List<String> keyList) {
        VariableQueryParamDto variableQueryParamDto = new VariableQueryParamDto();
        variableQueryParamDto.setKeyList(keyList);
        variableQueryParamDto.setTaskId(taskId);

        return post(variableQueryParamDto, "/task/queryTaskVariables");

    }

    /**
     * 查询流程变量
     * 全部都是
     *
     * @return
     */
    public static Result<IndexPageStatistics> querySimpleData(String userId) {
        String s = get("/process-instance/querySimpleData?userId=" + userId );
        return JsonMapperUtils.getInstance().parseObject(s, new TypeReference<Result<IndexPageStatistics>>() {});
    }

    /**
     * 创建流程
     *
     * @param node
     * @return
     */
	public static Result<String> createFlow(Node node, String userId) {
        String post = post(node, "/flow/create?userId=" + userId);
        Result<String> r = JsonMapperUtils.getInstance().parseObject(post, new TypeReference<Result<String>>() {});
        return r;

    }

    /**
     * 启动流程
     *
     * @param jsonObject
     * @return
     */
    public static String startProcess(ProcessInstanceParamDto jsonObject) {
        return post(jsonObject, "/flow/start");
    }


    /**
     * 查询指派任务
     *
     * @param jsonObject
     * @return
     */
    public static Result<PageResultDto<TaskDto>> queryAssignTask(TaskQueryParamDto jsonObject) {
        String post = post(jsonObject, "/flow/queryAssignTask");
        Result<PageResultDto<TaskDto>> r = JsonMapperUtils.getInstance().parseObject(post, new TypeReference<Result<PageResultDto<TaskDto>>>() {});
        return r;
    }

    /**
     * 查询已办任务
     *
     * @param jsonObject
     * @return
     */
    public static  Result<PageResultDto<TaskDto>> queryCompletedTask(TaskQueryParamDto jsonObject) {
        String post = post(jsonObject, "/flow/queryCompletedTask");
        Result<PageResultDto<TaskDto>> r = JsonMapperUtils.getInstance().parseObject(post, new TypeReference<Result<PageResultDto<TaskDto>>>() {});
        return r;
    }

    /**
     * 完成任务
     *
     * @param jsonObject
     * @return
     */
    public static Result<?> completeTask(TaskParamDto jsonObject) {
        String post = post(jsonObject, "/task/complete");
        return CommonUtil.toObj(post,Result.class);
    }

    /**
     * 转交
     *
     * @param jsonObject
     * @return
     */
    public static String setAssignee(TaskParamDto jsonObject) {
        return post(jsonObject, "/task/setAssignee");
    }

    /**
     * 终止流程
     *
     * @param jsonObject
     * @return
     */
    public static Result<String> stopProcessInstance(TaskParamDto jsonObject) {
        String post = post(jsonObject, "/flow/stopProcessInstance");
        Result<String> r = JsonMapperUtils.getInstance().parseObject(post, new TypeReference<Result<String>>() {});
        return r;
    }

    /**
     * 解决委派任务
     *
     * @param jsonObject
     * @return
     */
    public static String resolveTask(TaskParamDto jsonObject) {
        return post(jsonObject, "/task/resolveTask");
    }

    /**
     * 退回
     *
     * @param jsonObject
     * @return
     */
    public static String back(TaskParamDto jsonObject) {
        return post(jsonObject, "/task/back");
    }

    /**
     * 委派任务
     *
     * @param jsonObject
     * @return
     */
    public static String delegateTask(TaskParamDto jsonObject) {
        return post(jsonObject, "/task/delegateTask");
    }

    /**
     * 显示图片
     *
     * @param procInsId
     * @return
     */
    public static String showImg(String procInsId) {
        return get("/flow/showImg?procInsId=" + procInsId);
    }

    /**
     * 查询任务
     * @param taskId
     * @param userId
     * @return
     */
    public static  Result<TaskResultDto> queryTask(String taskId, String userId) {
        String s = get(StrUtil.format("/task/queryTask?taskId={}&userId={}", taskId, userId));
        Result<TaskResultDto> r = JsonMapperUtils.getInstance().parseObject(s, new TypeReference<Result<TaskResultDto>>() {});
        return r;
    }

}
